﻿using UnityEditor;
using UnityEngine;

namespace Core
{
    public static class GameInitEditor
    {
        [InitializeOnLoadMethod]
        public static void InitGame()
        {
            InitEditorWorld();

            EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
            EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
        }

        private static void InitEditorWorld()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
                return;

            Management.Dispose();
            Management.World.AddFeature<FrameFeatureEditor>();
            EditorApplication.update -= Update;
            EditorApplication.update += Update;
        }

        private static void EditorApplication_playModeStateChanged(PlayModeStateChange state)
        {
            switch (state)
            {
                case PlayModeStateChange.EnteredEditMode:
                    InitEditorWorld();
                    break;
                case PlayModeStateChange.ExitingEditMode:
                    EditorApplication.update -= Update;
                    break;
            }
        }

        public static void Update()
        {
            Management.World.Tick();
            Management.World.TickFix();
        }
    }
}
